package com.leyou.page.service.impl;

import com.leyou.item.pojo.*;
import com.leyou.page.client.BrandClient;
import com.leyou.page.client.CategoryClient;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecificationClient;
import com.leyou.page.service.PageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PageServiceImpl implements PageService {

    @Autowired
    TemplateEngine engine;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecificationClient specificationClient;

    @Override
    public Map<String, Object> querySpuBySpuId(Long spuId) {

        // 构建模型容器
        Map<String, Object> map = new HashMap<>();

        // spu
        SpuBo spuBo = goodsClient.querySpuBySpuId(spuId);
        String title = spuBo.getTitle();
        String subTitle = spuBo.getSubTitle();

        // spuDetail
        SpuDetail detail = spuBo.getSpuDetail();

        // skus
        List<Sku> skus = spuBo.getSkus();

        // 分类
        List<Category> categories = categoryClient.queryCategoryByIds(Arrays.asList(spuBo.getCid1(), spuBo.getCid2(), spuBo.getCid3()));

        // 品牌
        Brand brand = brandClient.queryBrandById(spuBo.getBrandId());

        // 规格组以及组内规格参数
        List<SpecGroup> specs = specificationClient.querySpecGroupsByCid(spuBo.getCid3());

        map.put("categories", categories);
        map.put("brand", brand);
        map.put("title", title);
        map.put("subTitle", subTitle);
        map.put("detail", detail);
        map.put("skus", skus);
        map.put("specs", specs);
        return map;
    }

    public void createHtml(Long spuId) {

        // 上下文
        Context context = new Context();
        context.setVariables(querySpuBySpuId(spuId));

        // 准备文件
        File destFile = deleteHtml(spuId);

        // 创建流,关联目标文件
        try (PrintWriter writer = new PrintWriter(destFile, "UTF-8")) {
            engine.process("item", context, writer);
        } catch (Exception e) {
            log.error("[静态页面服务] 创建静态页面失败,spuId:{}", spuId, e);
        }
    }


    public File deleteHtml(Long spuId) {
        File dest = getDestFile(spuId);
        if (dest.exists()) {
            dest.delete();
        }
        return dest;
    }

    private File getDestFile(Long spuId) {

        // 寻找目录
        File dir = new File("F:\\upload");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return new File(dir, spuId + ".html");
    }
}
